package com.leetcode;

import ds.TreeNode;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No543
 * @description 543. 二叉树的直径
 * @date 2021/3/14 下午5:35
 */
public class No543 {

    private static int max = 0;

    public static void main(String[] args) {
        /**
         *                1
         *               / \
         *              2   3
         *                 /
         *                5
         */
        TreeNode root = new TreeNode(1);
        TreeNode t1 = new TreeNode(2);
        TreeNode t2 = new TreeNode(3);
        TreeNode t3 = new TreeNode(5);
        root.left = t1;
        root.right = t2;
        t2.left = t3;
        diameterOfBinaryTree(root);
        System.out.println(max);

    }

    public static int diameterOfBinaryTree(TreeNode root) {
        if(root == null){
            return 0;
        }
        int leftHeight = diameterOfBinaryTree(root.left);
        int rightHeight = diameterOfBinaryTree(root.right);
        max = Math.max(max,leftHeight + rightHeight);
        return Math.max(leftHeight,rightHeight) + 1;
    }

}
